//
//  kWListsCell.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/5/25.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "kWListsCell.h"
#import "KWeiSectionView.h"
#import "kWListsItem.h"


static NSString * const kW_list_Id               = @"kW_list_Identifier";
#define ItemView_H HEIGHT * 0.71

@interface kWListsCell()<UICollectionViewDelegate,UICollectionViewDataSource>


@property(nonatomic,strong)KWeiSectionView            *sectionView;
@property(nonatomic,strong)UICollectionView           *listView;
@property(nonatomic,strong)UICollectionViewFlowLayout *layout;

@end

@implementation kWListsCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self buildSubViews];
    }
    return self;
}

- (void)buildSubViews
{
    [self.contentView addSubview:self.sectionView];
    [self.contentView addSubview:self.listView];
    
}

#pragma mark datasource delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    kWListsItem*cell = [collectionView dequeueReusableCellWithReuseIdentifier:kW_list_Id forIndexPath:indexPath];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(1,0,0,0);
}

#pragma mark lazy
- (KWeiSectionView *)sectionView
{
    if (!_sectionView) {
        _sectionView = [[KWeiSectionView alloc] initWithFrame: CGRectMake(0,0,WIDTH,ItemView_H * 0.09)];
        _sectionView.titleLabel.text = @"商品列表";
        _sectionView.englishLabel.text = @"Product list";
    }
    return _sectionView;
}

- (UICollectionViewFlowLayout *)layout
{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout  alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _layout.minimumLineSpacing = 1;
        _layout.minimumInteritemSpacing = 1;
        CGFloat itemW = WIDTH/2-0.5;
        CGFloat itemH = (ItemView_H - _sectionView.Height)/2-0.5;
        _layout.itemSize = CGSizeMake(itemW,itemH);
    }
    return _layout;
}

- (UICollectionView *)listView
{
    if (!_listView) {
        _listView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(_sectionView.frame), WIDTH,ItemView_H * 0.91) collectionViewLayout:self.layout];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.backgroundColor = BACKGROUND_COLOR;
        _listView.showsVerticalScrollIndicator = false;
        _listView.scrollEnabled = false;
        [_listView registerClass:[kWListsItem class] forCellWithReuseIdentifier:kW_list_Id];
    }
    return _listView;
}


@end
